package com.liu.nb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.nb.dto.DebugQueryDto;
import com.liu.nb.entity.*;
import com.liu.nb.mapper.ReceiptpatrolMapper;
import com.liu.nb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 巡逻回执任务表 服务实现类
 * </p>
 *
 * @author yzl
 * @since 2021-08-31
 */
@Service
public class ReceiptpatrolServiceImpl extends ServiceImpl<ReceiptpatrolMapper, Receiptpatrol> implements IReceiptpatrolService {

    /*@Autowired
    private ReceiptpatrolMapper receiptpatrolMapper;*/
    @Resource
    private ReceiptpatrolMapper receiptpatrolMapper;
    @Autowired
    private IPatroltaskService patroltaskService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDebugtaskService debugtaskService;
    @Autowired
    private ICircuitryService circuitryService;
    @Autowired
    private IDebugtaskTypeService debugtaskTypeService;
    @Autowired
    private IBugLevelService bugLevelService;
    @Override
    public Integer savaReceipt(Receiptpatrol receiptpatrol) {
        return receiptpatrolMapper.insert(receiptpatrol);
    }

    @Override
    public Receiptpatrol getReceipt(String ptid, String pid) {
        return receiptpatrolMapper.selectOne(new QueryWrapper<Receiptpatrol>()
                .eq("ptid", ptid)
                .eq("pid", ptid));
    }

    @Override
    public Integer updateReceipt(Receiptpatrol receiptpatrol) {
        return receiptpatrolMapper.updateById(receiptpatrol);
    }

    @Override
    public IPage<Receiptpatrol> getReceiptAll(Receiptpatrol receiptpatrol, String cid, Integer pageNo, Integer size) {
        QueryWrapper<Receiptpatrol> queryWrapper = new QueryWrapper<>();
        if(receiptpatrol!=null){
            queryWrapper =
                    new QueryWrapper<Receiptpatrol>().like(StrUtil.isNotBlank(receiptpatrol.getPtid()), "ptid", receiptpatrol.getPtid())
                            .eq(ObjectUtil.isNotNull(receiptpatrol.getBugId()), "bugId", receiptpatrol.getBugId())
                            .eq(ObjectUtil.isNotNull(receiptpatrol.getLevel()), "level", receiptpatrol.getLevel())
                            .le(ObjectUtil.isNotNull(receiptpatrol.getFindLastTime()), "findLastTime", receiptpatrol.getFindLastTime())
                            .ge(ObjectUtil.isNotNull(receiptpatrol.getFindFirstTime()), "findFirstTime", receiptpatrol.getFindLastTime())
                            .le(ObjectUtil.isNotNull(receiptpatrol.getIssueLastTime()), "issueLastTime", receiptpatrol.getIssueLastTime())
                            .ge(ObjectUtil.isNotNull(receiptpatrol.getIssueFirstTime()), "issueFirstTime", receiptpatrol.getIssueFirstTime())
                            .inSql("ptid","select id from patroltask where cid like '%"+cid +"%'");
        }else {
            queryWrapper = new QueryWrapper<Receiptpatrol>();
        }

        Page<Receiptpatrol> page = this.page(new Page<>(pageNo, size), queryWrapper);
        page.getRecords().forEach(i -> {
            Patroltask patroltask = patroltaskService.getById(i.getPtid());
            i.setPatroltask(patroltask);
            i.setSysUser(userService.getById(i.getUid()));
            patroltask.setUser(userService.getById(patroltask.getAdminUid()));
        });
        return page;
    }

    @Override
    public IPage<Receiptpatrol> getBylevel(Receiptpatrol receiptpatrol, Integer pageNo, Integer pageSize) {
        Page<Receiptpatrol> page=new Page<>(pageNo,pageSize);
        return receiptpatrolMapper.getBylevel(page,receiptpatrol);
    }

    @Override
    public IPage<DebugQueryDto> getDebugQuery(Integer current, Integer size, HashMap<String,String> map) {
        QueryWrapper <Receiptpatrol> patrolWrapper=new QueryWrapper<>();
        patrolWrapper
                .eq(StrUtil.isNotBlank(map.get("level")),"level",map.get("level"))
                .eq(StrUtil.isNotBlank(map.get("bugType")),"bug_id",map.get("bugType"))
        ;
        Page<Receiptpatrol> page = this.page(new Page<>(current,size),patrolWrapper);
        page.getRecords().forEach(i->{
            Debugtask debugtask = debugtaskService.getOne(new QueryWrapper<Debugtask>()
                    .inSql("id", "SELECT debug_id FROM debugtask_pole WHERE pid ='" + i.getPid() + "'")
                    .like(StrUtil.isNotBlank(map.get("taskNum")),"id",map.get("debug_id"))
                    .eq(StrUtil.isNotBlank(map.get("taskStatus")),"state",map.get("taskStatus"))
                    .eq(StrUtil.isNotBlank(map.get("gongZuoDanJu")),"type",map.get("gongZuoDanJu"))
                    .between(StrUtil.isNotBlank(map.get("startTime")),"time",map.get("startTime"),map.get("endTime"))
            );
            if (debugtask!=null) {
                SysUser sysUser = userService.getOne(new QueryWrapper<SysUser>().eq("id", debugtask.getCreateuser()));
                debugtask.setUser(sysUser);
                DebugtaskType type = debugtaskTypeService.getOne(new QueryWrapper<DebugtaskType>().eq("id", debugtask.getType()));
                debugtask.setDebugtaskType(type);
                BugLevel levelId = bugLevelService.getOne(new QueryWrapper<BugLevel>().eq("level_id", i.getLevel()));
                debugtask.setBugLevel(levelId);
                i.setDebugtask(debugtask);
                List<Circuitry> circuitries = circuitryService.list(new QueryWrapper<Circuitry>().inSql("id", "SELECT circuity_id FROM circuitry_pole WHERE pole_id = '" + i.getPid() + "'"));
                List<String> collect = circuitries.stream().map(ii -> ii.getId()).collect(Collectors.toList());
                i.setCid(collect);
                SysUser find = userService.getOne(new QueryWrapper<SysUser>().eq("id", i.getFindpeople()));
                i.setFindPeoplePojo(find);
            }else {
                page.setTotal(page.getTotal()-1);
            }
        });
        IPage<DebugQueryDto> queryDtoIPage=new Page<>(current,size);
        queryDtoIPage.setTotal(page.getTotal());
        List<DebugQueryDto> list=new ArrayList<>();
        page.getRecords().forEach(i ->{
            if (i.getDebugtask()!=null) {
                DebugQueryDto dto = new DebugQueryDto();
                dto.setBugType(i.getDebugtask().getDebugtaskType().getTypeName());
                dto.setTaskNum(i.getDebugtask().getId());
                dto.setTaskStatus(i.getDebugtask().getState());
                dto.setGongZuoDanJuStr(i.getDebugtask().getDebugtaskType().getTypeName());
                dto.setCid(i.getCid());
                dto.setPid(i.getPid());
                dto.setBugType(i.getDebugtask().getBugLevel().getBugLevel());
                dto.setFindPeople(i.getFindPeoplePojo().getName());
                dto.setFindTime(i.getTime());
                dto.setCreateUser(i.getDebugtask().getUser().getName());
                dto.setDownTime(i.getDebugtask().getTime());
                dto.setIntact(i.getIntact());
                dto.setDescription(i.getDescription());
                list.add(dto);
            }
        });
        queryDtoIPage.setRecords(list);
        return queryDtoIPage;
    }
}
